package vn.huync.smsbackupandrestore;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.nch.smsbackupandrestore.R;

public class ListFileActivity extends Activity {
	
	ListView listView;
	ViewFileAdapter viewFileAdapter;
	String[] listFile;
	MyApp myApp;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_file);
		
		myApp = (MyApp)getApplicationContext();
		listView = (ListView)findViewById(R.id.l_viewfile);
		
		
		Thread loadFileThread = new Thread(new Runnable() {
			
			public void run() {
				File dir = new File(myApp.appFolderPath);
				listFile = dir.list(new FilenameFilter() {
					
					public boolean accept(File dir, String filename) {
						if(filename.endsWith(".xml"))
							return true;
						else return false;
					}
				});
				
				viewFileAdapter = new ViewFileAdapter(getApplicationContext(), listFile);
				
				runOnUiThread(new Runnable() {
					
					public void run() {
						listView.setAdapter(viewFileAdapter);
					}
				});
				
			}
		});
		loadFileThread.start();
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(getApplicationContext(), ListSmsByAddressActivity.class);
				intent.putExtra("filePath", myApp.appFolderPath + listFile[position]);
				startActivity(intent);
			}
			
		});
		
		AdView adView = (AdView)this.findViewById(R.id.adView);
	    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
	    adRequestBuilder.addTestDevice("EEB3EC34691D85EFD1DF789EA3776326");
	    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
	    AdRequest adRequest = adRequestBuilder.build();
	    adView.loadAd(adRequest);
	}
	
	class ViewFileAdapter extends ArrayAdapter<String>{

		String[] fileNames;
		
		public ViewFileAdapter(Context context, String[] objects) {
			super(context, 0, objects);
			fileNames = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if(row == null){
				LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
				row = inflater.inflate(R.layout.row_simple, null);
				
				ImageView iconImageView = (ImageView)row.findViewById(R.id.t_rowsp_icon);
				iconImageView.setImageResource(R.drawable.view_icon);
			}
			
			TextView textView = (TextView)row.findViewById(R.id.t_rowsp_text);
			textView.setText(fileNames[position]);
			
			return row;
		}
		
	}
}
